
using System;
using System.Linq;
using Csla;
using Csla.Data;
using HousingInfo.DataLinQ;

namespace HousingInfo.Library
{ 
	public partial class ForumCommentBOList : Csla.BusinessListBase<ForumCommentBOList, ForumCommentBO>
    {
        #region ham lay tat ca cac binh luan cua dien dan theo ID
            public static ForumCommentBOList GetForumCommentBOList_ByID(int forumNewID,int sotrang, int soluong)
            {
                return DataPortal.Fetch<ForumCommentBOList>(new FilterCriteriaForumComment_ByID(forumNewID, sotrang, soluong));
            }
            private class FilterCriteriaForumComment_ByID
            {
                public int forumid;
                public int sotrang;
                public int soluong;
                public FilterCriteriaForumComment_ByID(int id, int st, int sl)
                {
                    this.forumid = id;
                    this.sotrang = st;
                    this.soluong = sl;
                }
            }
            private void DataPortal_Fetch(FilterCriteriaForumComment_ByID criteria)
            {
                using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                            .GetManager(Database.HousingInfoConnection))
                {
                    RaiseListChangedEvents = false;
                    if (criteria.soluong == -1 && criteria.sotrang == -1)
                    {
                        this.AddRange(
                            from row in mgr.DataContext.ForumCommemts
                            where row.ForumNewID == criteria.forumid
                            select ForumCommentBO.GetForumCommentBO(row.ForumCommentID)
                        );
                    }
                    else
                    {
                        this.AddRange(
                            (from row in mgr.DataContext.ForumCommemts
                            where row.ForumNewID == criteria.forumid
                            select ForumCommentBO.GetForumCommentBO(row.ForumCommentID)).Skip((criteria.sotrang - 1)*criteria.soluong).Take(criteria.soluong)
                        );
                    }

                    RaiseListChangedEvents = true;
                }//using


                //OnFetched();
            }
        #endregion

        #region Ham Kiem tra so luong comment theo id tin forum
        public static bool CheckForumComment_ByIDForum(int idforum)
        {
            int count = 0;
            using (var mgr = ContextManager<HousingInfoDataContext>
                    .GetManager(Database.HousingInfoConnection))
            {
                count = mgr.DataContext.ForumCommemts.Count(n => n.ForumNewID == idforum);
            }

            return (count > 0);
        }
        #endregion

        #region Ham dem so luong bai gui cua user
        public static int DemBaiGuiUser(string username)
        {
            int count = 0;
            using (var mgr = ContextManager<HousingInfoDataContext>
                    .GetManager(Database.HousingInfoConnection))
            {
                count = mgr.DataContext.ForumCommemts.Count(n => n.UserName == username);
                count += mgr.DataContext.ForumNews.Count(m => m.UserName == username);
            }

            return count;
        }
        #endregion

            #region Authorization Rules

            protected static void AddObjectAuthorizationRules()
		{
			//TODO: Define object-level authorization rules of ForumCommentBOList
			//AuthorizationRules.AllowCreate(typeof(ForumCommentBOList), "ForumCommentBOListCreateGroup"));
			//AuthorizationRules.AllowEdit(typeof(ForumCommentBOList), "ForumCommentBOListEditGroup"));
			//AuthorizationRules.AllowDelete(typeof(ForumCommentBOList), "ForumCommentBOListDeleteGroup"));
		}
		#endregion //Authorization Rules

        #region Ham dem so luong tin Comment theo id tin
        public static int CountTotalForumNews_BySubID(int newsForumID)
        {
            int count = 0;
            using (var mgr = ContextManager<HousingInfoDataContext>
                    .GetManager(Database.HousingInfoConnection))
            {
                count = mgr.DataContext.ForumCommemts.Count(n => n.ForumNewID == newsForumID);
            }

            return count;
        }
        #endregion // end kiem tra so luong tin
    }
}
